package br.ceut.especializacao.poo.fornecedores;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import br.ceut.especializacao.poo.util.PersistenceMechanismException;
import br.ceut.especializacao.poo.util.PersistenceMechanismRDBMS;
import br.ceut.especializacao.poo.util.RepositorioException;

public class RepositorioEnderecosBDR implements RepositorioEnderecos {

    private static RepositorioEnderecosBDR instance;
    private PersistenceMechanismRDBMS pm;
	
    private RepositorioEnderecosBDR() {
        try {
            pm = PersistenceMechanismRDBMS.getInstance();
            pm.connect();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public synchronized static RepositorioEnderecosBDR getInstance() {
        if (instance == null) {
            instance = new RepositorioEnderecosBDR();
        }
        return instance;
    }

    public int inserir(Endereco endereco) throws RepositorioException {
    	int idEndereco = 0;
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            
            idEndereco = statement.executeUpdate("INSERT INTO ENDERECO VALUES (" +
            		"'" + endereco.getLogradouro() + "', '" +
            		"'" + endereco.getNumero() + "', '" +
            		"'" + endereco.getBairro() + "', '" +
            		"'" + endereco.getCep() + "', '" +
            		"'" + endereco.getCidade() + "', '" +
            			  endereco.getEstado() + "')");
        } catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
            throw new RepositorioException(e);
	    } finally {
		    try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
			return idEndereco;
        }
    }

	public void atualizar(Endereco endereco) throws RepositorioException {
	    try {
    	    Statement statement = (Statement) pm.getCommunicationChannel();
            int i = statement.executeUpdate("UPDATE ENDERECO SET " +
            		" logradouro = " + endereco.getLogradouro() +
            		" numero = " + endereco.getNumero() +
            		" bairro = " + endereco.getBairro() +
            		" cep = " + endereco.getCep() +
            		" cidade = " + endereco.getCidade() +
            		" estado = " + endereco.getEstado() +
           	        " WHERE idEndereco = " + endereco.getIdEndereco() + "");
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
		} catch (SQLException e) {
		    throw new RepositorioException(e);
	    } finally {
	    	try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
	}

	public boolean existe(String numero) throws RepositorioException {
        boolean resposta = false;
        try {
            Statement statement = (Statement) pm.getCommunicationChannel();
            ResultSet resultset = statement.executeQuery("SELECT * FROM ENDERECO WHERE numero = '" + numero + "'");
            resposta = resultset.next();
			resultset.close();
		} catch(PersistenceMechanismException e){
            throw new RepositorioException(e);
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
        return resposta;
    }

//    public RepositorioContas getContas() throws RepositorioException {
//        RepositorioContas resposta = new RepositorioContasArray();
//        try {
//            Statement statement = (Statement) pm.getCommunicationChannel();
//            ResultSet resultset = statement.executeQuery("SELECT * FROM ENDERECO");
//            while (resultset.next()) {
//                Conta conta = new Conta(resultset.getString("numero"), resultset.getDouble("saldo"));
//                resposta.inserir(conta);
//            }
//			resultset.close();
//		} catch(PersistenceMechanismException e){
//            throw new RepositorioException(e);
//        } catch (SQLException e) {
//			throw new RepositorioException(e);
//		} finally {
//			try {
//				pm.releaseCommunicationChannel();
//			} catch (PersistenceMechanismException ex) {
//				throw new RepositorioException(ex);
//			}
//		}
//        return resposta;
//    }

    public IteratorFornecedores getIterator() throws RepositorioException {
        return this.getIterator();
    }

}